export class WxRequest {
  //默认参数
  defaults = {
    baseUrl: '',
    url: '',
    data: null,
    method: 'get',
    header: {
      'Content-type': 'application/json'
    },
    timeout: 60000,
    isLoading: true //是否有加载特效
  }
  //拦截器
  interceptors = {
    request: (config: any) => config,
    response: (response: any) => response
  }
  //调用队列
  queue = [] as string[]
  timerId = 0

  // 构造函数
  constructor(params = {}) {
    //合并到默认参数
    this.defaults = Object.assign({}, this.defaults, params)
  }
  //request  请求
  request(options: any) {
    //拼接url
    options.url = this.defaults.baseUrl + options.url
    //把传过来的参数和默认参数合并一起进行请求
    options = { ...this.defaults, ...options }
    if (options.isLoading && options.method !== 'upload') {
      //清除定时器
      this.timerId && clearTimeout(this.timerId)
      this.queue.length === 0 && wx.showLoading({
        title: "加载中..."
      })
      this.queue.push('request')
    }
    this.interceptors.request(options)
    return new Promise<Object>((resolve, reject) => {
      if (options.method === 'upload') {
        wx.uploadFile({
          ...options,
          success: (res) => {
            res.data = JSON.parse(res.data)
            const mergeRes = Object.assign({}, res, { config: options })
            resolve(this.interceptors.response(mergeRes))
          },
          fail: (err) => {
            const mergeErr = Object.assign({}, err, { config: options })
            reject(this.interceptors.response(mergeErr))
          }
        })
      } else {
        wx.request({
          ...options,
          success: (res) => {
            resolve(this.interceptors.response(Object.assign({}, res, { config: options })))
          },
          fail: (err) => {
            reject(this.interceptors.response(Object.assign({}, err, { config: options })))
          },
          complete: () => {
            if (options.isLoading) {
              this.queue.pop()
              this.queue.length === 0 && this.queue.push('request')
              this.timerId = setTimeout(() => {
                this.queue.pop()
                this.queue.length === 0 && wx.hideLoading()
                clearTimeout(this.timerId)
              }, 1);
            }
          }
        })
      }
    })
  }
  /**
   *  文件上传
   * @param url 接口地址
   * @param filePath 文件地址
   * @param name 上传名称
   * @param config  配置
   */
  upload(url: string, filePath: string, name = 'file', config = {}) {
    return this.request(Object.assign({ url, filePath, name, method: 'upload' }, config))
  }
  /**
   * get 方法
   * @param url 接口地址
   * @param data 接口参数
   * @param config 配置
   */
  get(url: string, data = {} as any, config = {} as any) {
    return this.request(Object.assign({ url, data, method: 'get' }, config))
  }
  /**
    * delete 方法
    * @param url 接口地址
    * @param data 接口参数
    * @param config 配置
    */
  delete(url: string, data = {} as any, config = {} as any) {
    return this.request(Object.assign({ url, data, method: 'delete' }, config))
  }
  /**
    * post 方法
    * @param url 接口地址
    * @param data 接口参数
    * @param config 配置
    */
  post(url: string, data = {} as any, config = {} as any) {
    return this.request(Object.assign({ url, data, method: 'post' }, config))
  }
  /**
    * put 方法
    * @param url 接口地址
    * @param data 接口参数
    * @param config 配置
    */
  put(url: string, data = {} as any, config = {} as any) {
    return this.request(Object.assign({ url, data, method: 'put' }, config))
  }
  //并发处理 instance.all(instance.get(),instance.post)
  all( ...promise: Promise<Object>[]) {
    return Promise.all(promise)
  }
}